package com.huang.interrupt;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/*
*    interrupt 线程标志位 false  ->true   t1线程结束后标志位复位false
*       而interrupted() 静态方法 是返回标志位 后立马复位false
*
* */
public class one {
    private static volatile boolean isInterupt = false;
    static AtomicBoolean fla=new AtomicBoolean(false);


    public static void main(String[] args) throws InterruptedException {

    m3();



    }
    public static void m3() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("ture了");
                    break;
                }
                // 如果 线程在sleep的时候  打断会报错 标志位会从true--》false
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    //需要  在异常的时候  在打断一下  将false---true
                    Thread.currentThread().interrupt();

                    e.printStackTrace();
                }

                System.out.println("false");

            }

        },"t1");
        t1.start();
        System.out.println(t1.isInterrupted()+"1111");

        TimeUnit.SECONDS.sleep(2);

        new Thread(()->{
            System.out.println("改变了。。。。");
            t1.interrupt();

        }).start();


//        System.out.println(t1.isInterrupted()+"0000");
//        System.out.println(t1.isInterrupted()+"0000");
//        System.out.println(t1.isInterrupted()+"0000");

    }

    public static void m2() throws InterruptedException {
        new Thread(()->{
            while (true){
                if(fla.get()){
                    System.out.println("ture了");
                    break;
                }
                System.out.println("false");

            }

        },"a").start();
        TimeUnit.SECONDS.sleep(2);
        new Thread(()->{
            fla.set(true);

        },"b").start();
    }


    public static void m1() throws InterruptedException {
        new Thread(()->{
            while (true){
                if(isInterupt){
                    System.out.println("ture了");
                    break;
                }
                System.out.println("false");

            }

        },"a").start();
        TimeUnit.SECONDS.sleep(2);
        new Thread(()->{
            isInterupt=true;

        },"b").start();
    }

}
